package com.eattravel.controller;

import com.eattravel.cache.RedisUtils;
import com.eattravel.pojo.Commodity;
import com.eattravel.pojo.OrderCommodity;
import com.eattravel.service.CommodityService;
import com.eattravel.vo.AreaFood;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * zx 2019.9.21
 * 关于商品信息的管理
 */
@Controller
@RequestMapping("commodity")
public class CommodityController {
    @Autowired
    CommodityService commodityService;

    @Autowired
    RedisUtils redisUtils;

    @RequestMapping("findCommById")
    @ResponseBody
    public Commodity findCommById(String commodityId) {
        Commodity commodity = commodityService.findCommById(commodityId);
        redisUtils.sSet("123", commodity);
        return commodity;
    }

    /**
     * 通过订单的Id号查找
     *
     * @param orderId
     * @return
     */
    @RequestMapping("findCommodityByOrderId")
    @ResponseBody
    public List<Commodity> findCommodityByOrderId(String orderId) {


        List<Commodity> commodityList = commodityService.findCommodityByOrderId(orderId);
        return commodityList;

    }


    /**
     * 通过订单的Id号查找
     *
     * @param orderId
     * @return
     */
    @RequestMapping("findCommodityByOrderId1")
    @ResponseBody
    public List<OrderCommodity> findCommodityByOrderId1(String orderId) {


        List<OrderCommodity> commodityList = commodityService.findCommodityByOrderId1(orderId);
        return commodityList;

    }
    /**
     * 通过商品的Id修改
     * @param
     * @return
     */
    @RequestMapping("updataComm")
    @ResponseBody
    public String updataComm(String[] commodityIds, String[]commGrade){
        if(null==commodityIds||commGrade==null){
            return "输入格式不正确";
        }

        for(int i=0;i<commodityIds.length;i++){
            System.out.println(commodityIds[i]);
        }
        for(int i=0;i<commGrade.length;i++){
            System.out.println(commGrade[i]);
        }

        for(int i=0;i<commodityIds.length;i++){
            for (int j=0;j<commGrade.length;j++){
                if(i==j){
                    float aa= Float.parseFloat(commGrade[j]);
                    float grade= (float)(Math.round(aa*100))/100;
                    int aaa=commodityService.updataComm(commodityIds[i],grade);

                }


            }
        }



        return "123";
    }

}
